package com.jiantong.work.impl.service;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.work.enums.ButcherReportCertStatus;
import com.jiantong.work.impl.dao.ButcherReportCertDao;
import com.jiantong.work.model.ButcherReportCert;
import com.jiantong.work.service.ButcherReportCertReadService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import io.terminus.common.model.Response;

import java.util.List;
import java.util.Map;

/**
 * Created by mz on 17/5/17.
 *
 */
@Slf4j
@Service
@RpcProvider
public class ButcherReportCertReadServiceImpl implements ButcherReportCertReadService {

    private final ButcherReportCertDao butcherReportCertDao;

    @Autowired
    public ButcherReportCertReadServiceImpl(ButcherReportCertDao butcherReportCertDao){
        this.butcherReportCertDao = butcherReportCertDao;
    }

    @Override
    public Response<ButcherReportCert> findButcherReportCertById(Long id) {
        try{
            ButcherReportCert butcherReportCert = butcherReportCertDao.findById(id);
            if(butcherReportCert == null){
                log.error("butcher report cert is not found");
                return Response.fail("butcher report cert is not found");
            }
            return Response.ok(butcherReportCert);
        }catch (Exception e){
            log.error("failed to find butcher report cert id = {}",id, Throwables.getStackTraceAsString(e));
            return Response.fail("failed to find butcher report cert");
        }
    }

    @Override
    public Response<Paging<ButcherReportCert>> pagingButcherReportCert(Integer pageNo,
                                                                       Integer pageSize,
                                                                       Long reportId,
                                                                       String reportNumber) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo,pageSize);
            Map<String ,Object> criteria = Maps.newHashMap();
            if(reportId != null){
                criteria.put("reportId",reportId);
            }
            if(reportNumber != null){
                criteria.put("reportNumber",reportNumber);
            }
            criteria.put("status", ButcherReportCertStatus.HAS_PRINTED.value());
            return Response.ok(butcherReportCertDao.paging(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
        }catch (Exception e){
            log.error("failed to paging butcher report cert reportId = {},reportNumber = {}",reportId,reportNumber,Throwables.getStackTraceAsString(e));
            return Response.fail("failed to paging butcher report cert");
        }
    }

    @Override
    public Response<List<ButcherReportCert>> findListByReportId(Long reportId) {
        try{
            return Response.ok(butcherReportCertDao.findListByReportId(reportId));
        }catch (Exception e){
            log.error("failed to find butcher report cert reportId = {}",reportId, Throwables.getStackTraceAsString(e));
            return Response.fail("failed to find butcher report cert");
        }
    }
}
